//
//  WBComposePictureView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/18.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBComposePictureView.h"
#import <Masonry.h>

#pragma mark - 自定义cell类
@interface  WBComposePictureViewCell : UICollectionViewCell

//设置属性 外界赋值
@property (nonatomic , strong) UIImage *image;

//删除图片
@property (nonatomic , strong) UIButton *deleteBtn;

//定义闭包 删除按钮的时候 通过block告诉外界
@property (nonatomic , copy) void(^block)();

@end

@interface  WBComposePictureViewCell()

//背景图片
@property (nonatomic , strong) UIImageView *bgImageView;



@end

@implementation WBComposePictureViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self setupUI];
        
    }
    
    return self;
    
}

- (void)setImage:(UIImage *)image
{
    _image = image;
    
    if(image == nil)
    {
        self.bgImageView.image = [UIImage imageNamed:@"compose_pic_add"];
        self.bgImageView.highlightedImage = [UIImage imageNamed:@"compose_pic_add_highlighted"];
        
        self.deleteBtn.hidden = YES;
    
    }else
    {
         self.bgImageView.image = image;
        //cell重用问题
        self.bgImageView.highlightedImage = nil;
        
        self.deleteBtn.hidden = NO;
    }
    
   

}

- (UIImageView *)bgImageView
{
    if(!_bgImageView)
    {
        _bgImageView = [[UIImageView alloc] init];
    }
    
    return _bgImageView;
    
}

- (UIButton *)deleteBtn
{
    if(!_deleteBtn)
    {
        
        _deleteBtn = [[UIButton alloc] init];
        
        [_deleteBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
        //设置图片
        [_deleteBtn setImage:[UIImage imageNamed:@"compose_photo_close"] forState:UIControlStateNormal];
    
    }
    
    return _deleteBtn;

}

//删除按钮点击事件
- (void)btnClick
{
    NSLog(@"删除按钮点击了");
    
    //执行闭包
    self.block();
    
}


- (void)setupUI
{
    self.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];;
    
    //添加控件
    [self.contentView addSubview:self.bgImageView];
    [self.contentView addSubview:self.deleteBtn];
    //设置UI
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.contentView).mas_offset(UIEdgeInsetsZero);
        
    }];
    [self.deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.mas_equalTo(self.contentView);
    }];
    
}
@end

#pragma mark - 图片选择
@interface  WBComposePictureView()<UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation WBComposePictureView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if(self = [super initWithFrame:frame collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]])
    {
    
        [self setupUI];
    }
    
    return self;
}

- (NSMutableArray *)images
{
    if(!_images)
    {
        
        _images = [NSMutableArray array];
    
    }
    
    return _images;

}

- (void)addImage:(UIImage *)image
{
    
    [self.images addObject:image];
    
    //显示当前控件
    self.hidden = NO;
    
    
    //刷新数据
    [self reloadData];

}

#pragma mark - 设置UI
- (void)setupUI
{
    self.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    
    //隐藏当前控件
    self.hidden = YES;
    
    //设置代理
    self.dataSource = self;
    self.delegate = self;
    
    //输出cell
    [self registerClass:[WBComposePictureViewCell class] forCellWithReuseIdentifier:@"item"];
    
    //设置layout
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    
    //设置cell间距
    CGFloat margin = 5;
    
    //设置宽高
    CGFloat cellHW = (kScreenW - 20 - 2 * margin) / 3;
    
    //设置item
    layout.itemSize = CGSizeMake(cellHW, cellHW);
    
    //设置间距
    layout.minimumLineSpacing = margin;
    layout.minimumInteritemSpacing = margin;

}

#pragma mark - 数据源方法实现
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(self.images.count == 0 || self.images.count == 9)
    {
        return self.images.count;
    
    }
    
    return self.images.count + 1;
    
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WBComposePictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    
    if(indexPath.item == self.images.count)
    {
        
        cell.image = nil;
    
    }else
    {
     cell.image = self.images[indexPath.item];
        
        __weak typeof(self) weakSelf = self;
        cell.block = ^{
        
        //删除当前的图片 刷新tableview
            [weakSelf.images removeObjectAtIndex:indexPath.item];
            
            if(weakSelf.images.count == 0)
            {
                weakSelf.hidden = YES;
            }
            
            [weakSelf reloadData];
        
        };
    
    }
    
   
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self deselectItemAtIndexPath:indexPath animated:YES];
    
    if(indexPath.item == self.images.count)
    {
        //说明点击的是最后的加号 告诉外界去选择图片
        self.block();
    
    }
    

}




@end














